<?php
namespace app\admin\controller;

class Menu extends Base
{
    private $tn = 'menu';

    public function index()
    {
        if (isset($this->get['page']) && isset($this->get['limit'])) {
            $where = [];
            if (isset($this->get['parent_id'])) {
                $where['parent_id'] = $this->get['parent_id'];
            }
            if (isset($this->get['module'])) {
                $where['module'] = $this->get['module'];
            }
            if (isset($this->get['name'])) {
                $where['name'] = ['like', '%' . $this->get['name'] . '%'];
            }
            $list = db($this->tn)->field(true)->where($where)->page($this->get['page'])->limit($this->get['limit'])->select();
            foreach ($list as $k => $v) {
                $list[$k]['parent_id'] = get_field('admin_menu', $v['parent_id'], 'name', '顶级菜单');
                $list[$k]['module']    = get_field('admin_module', $v['module'], 'route', 'admin');
                $list[$k]['icon']      = '<i class="fa ' . $v['icon'] . '"></i>';
            }
            $count = db($this->tn)->where($where)->count();
            $this->returnAPI('', 0, $list, ['count' => $count, 'limit' => $this->get['limit']]);
        }
        return $this->fetch('', [
            'parent_ids' => ['' => '父级菜单', '0' => '顶级菜单'] + db($this->tn)->where('parent_id', 0)->order(['sort' => 'desc'])->column('name', 'id'),
            'modules'    => ['' => '所属模块'] + db('module')->where('type', 0)->order(['route'])->column('route', 'id'),
        ]);
    }

    public function edit($id = 0)
    {
        $model = model($this->tn);
        if ($this->post) {
            if ($id) {
                $model = $model->get($id);
                if (is_null($model)) {
                    $this->returnAPI('信息有误，请稍后重试');
                }
            }
            $result = $model->validate($this->tn)->allowField(true)->save($this->post);
            if ($result !== false) {
                $this->returnAPI('操作成功', 0);
            }
            $this->returnAPI($model->getError());
        }
        $model = $model->get($id);
        if (is_null($model)) {
            $data = $this->table_fields($this->tn);
        } else {
            $data = $model->getData();
        }
        return $this->fetch('', [
            'data'       => $data,
            'parent_ids' => ['0' => '顶级菜单'] + db($this->tn)->where(['parent_id' => 0, 'route' => '', 'id' => ['<>', $id]])->order(['sort' => 'desc'])->column('name', 'id'),
            'routes'     => $this->routes(),
            'modules'    => db('module')->where('type', 0)->order(['route'])->column('route', 'id'),
        ]);
    }

    private function routes()
    {
        $routes  = [];
        $modules = db('module')->where('type', 0)->order(['route'])->column('route', 'id');
        foreach ($modules as $module) {
            $controllers = $this->controllers($module);
            foreach ($controllers as $controller) {
                $actions = $this->actions($module, $controller);
                foreach ($actions as $action) {
                    $routes[] = '/' . $module . '/' . hump_underline($controller) . '/' . $action;
                }
            }
        }
        return $routes;
    }

    private function controllers($module)
    {
        $controllers = glob(ROOT_PATH . 'application' . DS . $module . DS . 'controller' . DS . '*.php');
        foreach ($controllers as $k => $v) {
            $controller = basename($v, '.php');
            if ($controller === 'Base') {
                unset($controllers[$k]);
            } else {
                $controllers[$k] = $controller;
            }
        }
        return array_values($controllers);
    }

    private function actions($module, $controller)
    {
        $content = file_get_contents(ROOT_PATH . 'application' . DS . $module . DS . 'controller' . DS . $controller . '.php');
        $result  = [];
        preg_match_all('/.*?public.*?function(.*?)\(.*?\)/i', $content, $result);
        $actions = $result[1];
        foreach ($actions as $k => $v) {
            $action = trim($v);
            if ($action === '') {
                unset($actions[$k]);
            } else {
                $actions[$k] = $action;
            }
        }
        return array_values($actions);
    }
}
